﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EtlComm
{
    /// <summary>
    /// 任务队列
    /// </summary>
    [Serializable]
    public class TaskQueue
    {
        private Queue<TaskObject> _queue;
        private static object _lock = new object();

        public IList<TaskObject> lstTaskObject
        {
            get
            {
                TaskObject[] arr = new TaskObject[_queue.Count];
                _queue.CopyTo(arr, 0);
                return arr.ToList();
            }
        }

        public TaskQueue()
        {
            _queue = new Queue<TaskObject>();
        }

        public TaskQueue(IList<TaskObject> list)
        {
            _queue = new Queue<TaskObject>(list.AsEnumerable());
        }

        /// <summary>
        /// 加入任务
        /// </summary>
        /// <param name="task"></param>
        public void Push(TaskObject task)
        {
            lock (_lock)
            {
                _queue.Enqueue(task);
                lstTaskObject.Add(task);
            }
        }
        /// <summary>
        /// 弹出任务
        /// </summary>
        /// <returns></returns>
        public TaskObject Pop()
        {
            TaskObject task;
            lock (_lock)
            {
                try
                {
                    task = _queue.Dequeue();
                }
                catch
                {
                    task = null;
                }

            }
            return task;
        }
        /// <summary>
        /// 任务数
        /// </summary>
        public int Count
        {
            get
            {
                int _count = 0;
                lock (_lock)
                {
                    _count = _queue.Count;
                }
                return _count;
            }
        }
    }
}
